<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 工程查看接口
 */
class Project extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
    * 获取工程列表
    */
    public function list(){
        $bh  = $this->request->request("bh");
        $page = $this->request->request('page');
        $ver = Db::name('projectdata')->where(['managerid'=>$bh,'gcstatus'=>'已下达竣工命令'])->order('id DESC')->limit(25)->page($page)
            ->field('id,Gcbh,Projectname,datatime,ProjectType')->select();
        
        if(!$ver) {
            $this->error('暂无下一页');
        }
        $this->success('获取成功', $ver);
    }

    /**
    * 获取工程详细信息
    */
    public function detail(){
        $gcbh  = $this->request->request("gcbh");
        //获取基本信息
        $basedata = Db::name('projectdata')->alias('a')->join('basedata b','a.managerid = b.Bh')->where(['a.Gcbh'=>$gcbh,'a.gcstatus'=>'已下达竣工命令','b.Datatype'=>'作业队'])->field('a.id,a.Gcbh,a.Projectname,a.jhdate,a.ProjectType,a.linename,a.Projectadd,b.Content1')->find();
        //获取作业点
        //$work = Db::name('zuoyesubdata')->where(['Gcbh'=>$gcbh])->select();
        //$workuser = Db::name('gc_pda_worker')->where(['gcbh'=>$gcbh])->field('datatype,pdabh,workername')->select();
        //$line_on = Db::name('linedata')->where(['gcbh'=>$gcbh,'linestatus'=>'挂接'])->field('ganhao,workerbh,linedate,linetime,linetype,linestatus')->select();
        //$line_off = Db::name('linedata')->where(['gcbh'=>$gcbh,'linestatus'=>'拆除'])->field('ganhao,workerbh,linedate,linetime,linetype,linestatus')->select();
        //$data  = array(
        //     'basedata' => $basedata,
        //     'work'=>$work,
        //     'workuser'=>$workuser,
        //     'line_on'=>$line_on,
        //     'line_off'=>$line_off
        // );
        if(!$basedata) {
            $this->error('查询失败，数据不存在');
        }
        $this->success('获取成功', $basedata);
    }
        /**
    * 获取作业点
    */
    public function work(){
        $gcbh  = $this->request->request("gcbh");
        //获取作业点
        $work = Db::name('zuoyesubdata')->where(['Gcbh'=>$gcbh])->select();
        if(!$work) {
            $this->error('查询失败，数据不存在');
        }
        $this->success('获取成功', $work);
    }
        /**
    * 进场信息
    */
    public function workuser(){
        $gcbh  = $this->request->request("gcbh");
        $workuser = Db::name('gc_pda_worker')->where(['gcbh'=>$gcbh])->field('datatype,pdabh,workername')->select();
        if(!$workuser) {
            $this->error('查询失败，数据不存在');
        }
        $this->success('获取成功', $workuser);
    }
        /**
    * 地线挂接信息
    */
    public function lineon(){
        $gcbh  = $this->request->request("gcbh");
        $line_on = Db::name('linedata')->where(['gcbh'=>$gcbh,'linestatus'=>'挂接'])->field('ganhao,workerbh,linedate,linetime,linetype,linestatus')->select();
        $a = array();
        foreach ($line_on as $key => $value) {
            # code...
            $a[$value['ganhao']] = $value;
        }
        $b = [];
        foreach ($a as $key => $value) {
            # code...
            array_push($b, $value);
        }
        if(!$b) {
            $this->error('查询失败，数据不存在');
        }
        $this->success('获取成功', $b);
    }
        /**
    * 地线拆除信息
    */
    public function lineoff(){
        $gcbh  = $this->request->request("gcbh");
        $line_off = Db::name('linedata')->where(['gcbh'=>$gcbh,'linestatus'=>'拆除'])->field('ganhao,workerbh,linedate,linetime,linetype,linestatus')->select();
        $a = array();
        foreach ($line_off as $key => $value) {
            # code...
            $a[$value['ganhao']] = $value;
        }
        $b = [];
        foreach ($a as $key => $value) {
            # code...
            array_push($b, $value);
        }
        if(!$b) {
            $this->error('查询失败，数据不存在');
        }
        $this->success('获取成功', $b);
    }

        /**
    * 开工申请信息
    */
    public function open(){
        $gcbh  = $this->request->request("gcbh");
        //获取作业点信息,
        $data = Db::query( "SELECT COUNT(*) as number,COUNT(xuke = 'Y' or null) as xuke,COUNT(kaiguan = 'Y' or null) as kaiguan ,COUNT(jiedi = 'Y' or null) as jiedi,COUNT(jiaokua = 'Y' or null) as jiaokua,COUNT(bingxing = 'Y' or null) as bingxing,COUNT(lujing = 'Y' or null) as lujing FROM zuoyesubdata WHERE Gcbh = ? " ,[$gcbh]);
        $baogao = Db::name('startorfinishdata')->where(['Gcbh'=>$gcbh,'Sqmd'=>'开工'])->field('Baogaodate,Baogaotime,Zyrenwu')->find();
        if(!$baogao) {
            $this->error('查询失败，数据不存在');
        }

        $this->success('获取成功', $baogao+$data[0]);
    }

    /**
    * 获取险情
    */
    public function danger(){
        $gcbh  = $this->request->request("gcbh");
        //获取作业点信息,
        $danger = Db::name('apptempdata')->where(['Gcbh'=>$gcbh,'flag'=>'danger'])->field('txt')->select();
        if(!$danger) {
            $this->error('查询失败，数据不存在');
        }
        $data  = array( );
        foreach ($danger as $key => $value) {
            $b  = array();
            $a = explode('$', $value['txt']);
            $b['type'] = $a[6];
            $b['ganhao'] = $a[3];
            $b['date'] = $a[4];
            $b['time'] = $a[5];
            $b['worker'] = $a[9];
            array_push($data, $b);
        }

        $this->success('获取成功', $data);
    }

    public function close(){
        $gcbh  = $this->request->request("gcbh");
        //获取作业点信息,
        $data = Db::query( "SELECT COUNT(*) as number,COUNT(xuke = 'Y' or null) as xuke,COUNT(kaiguan = 'Y' or null) as kaiguan ,COUNT(jiedi = 'Y' or null) as jiedi,COUNT(jiaokua = 'Y' or null) as jiaokua,COUNT(bingxing = 'Y' or null) as bingxing,COUNT(lujing = 'Y' or null) as lujing FROM zuoyesubdata WHERE Gcbh = ? " ,[$gcbh]);
        $baogao = Db::name('startorfinishdata')->where(['Gcbh'=>$gcbh,'Sqmd'=>'竣工'])->field('Baogaodate,Baogaotime,Zyrenwu')->find();
        if(!$baogao) {
            $this->error('查询失败，数据不存在');
        }

        $this->success('获取成功', $baogao+$data[0]);
    }





    /**
    工程前三步(脱机)数据上传(解析后数据demo)
    $data = [
        //projectdata表关键字段
        'bh'   =>'S20200309104437_X20200903165427_W20200903165954',//managerid,Sgdbh
        'Gcbh' =>'20200903188888',//工程编号
        'Projectname'=>'88千伏吴江', //工程名称
        'datatime' => '2020-11-13 ', //工程日期 
        'ProjectType'=>'新建',//工程类别
        'linename'=>'吴江线',//线路名称
        //'jhdate'=>'2020-09-03 8:0~2020-09-03 20:0',//计划时间 开工时间~竣工时间 2020-09-03 8:0~2020-09-03 20:0
        'startdate'=>'2020-09-03 8:0',
        'enddate'=>'2020-09-03 20:0',
        'Projectadd'=>'吴江',//工程地址
        'Createman'=>'吴江作业队',//创建人
        //zuoyedata表关键字段
        'xianqu'=>'吴江线', //xianqu,xiangzhen都是线路名称
        //zuoyesubdata 数据采集关键字段
        'subdata'=>[
            [
                'Gcbh'=>'20200903188888',//工程编号
                'ganhao'=>'吴江线_001',//杆塔号
                'Weidu'=>'39.8955321',//纬度
                'jingdu'=>'116.4077067',//经度
                'xuke'=>'Y',//许可点
                'caozuo'=>'Y',//操作点
                'jinzhi'=>'Y',//禁止点
                'duka'=>'Y',//危险点
                'kaiguan'=>'Y',//是否开关点
                'jiedi'=>'Y',//是否地线杆
                'zhixian'=>'N',//是否作业杆
                'jiaokua'=>'N',//是否交叉跨越点
                'bingxing'=>'N',//是否并行
                'lujing'=>'N',//是否班组地线杆
            ],
            [
                'Gcbh'=>'20200903188888',//工程编号
                'ganhao'=>'吴江线_002',//杆塔号
                'Weidu'=>'39.8951234',//纬度
                'jingdu'=>'116.4075678',//经度
                'xuke'=>'Y',//许可点
                'caozuo'=>'Y',//操作点
                'jinzhi'=>'Y',//禁止点
                'duka'=>'Y',//危险点
                'kaiguan'=>'N',//是否开关点
                'jiedi'=>'Y',//是否地线杆
                'zhixian'=>'N',//是否作业杆
                'jiaokua'=>'N',//是否交叉跨越点
                'bingxing'=>'N',//是否并行
                'lujing'=>'N',//是否班组地线杆
            ]
        ]
    ];

    */

    public function upload(){
        $jsondata  = $this->request->request("jsondata"); //json后的base64数据
        if(!$jsondata) {
            $this->error('未获取到工程脱机数据');
        }

        $data = json_decode(base64_decode($jsondata),true);
        if (!$data) {
            $this->error('工程脱机数据解析失败,请检查数据格式');
        }

        //检查工程编号是否存在与数据库
        $pd = Db::name('projectdata')->where('Gcbh',$data['Gcbh'])->find();
        if ($pd) {
            $this->error('当前工程编号已存在');
        }

        //生成townid
        $tmp = explode('_', $data['bh']); //如果编号格式不会会引发异常
        $townid =  $tmp[0].'_'.$tmp[1];

        //开启事务处理       
        Db::startTrans();

        //将数据写入projectdata表
        $projectdata = [
            'townid'      => $townid,
            'managerid'   => $data['bh'],
            'Gcbh'        => $data['Gcbh'],
            'subbh'       => '000',
            'Sgdbh'       => $data['bh'],
            'Projectname' => $data['Projectname'],
            'datatime'    => $data['datatime'],
            'ProjectType' => $data['ProjectType'],
            'linename'    => $data['linename'],
            'jhdate'      => $data['startdate'].'~'.$data['enddate'],
            'Projectadd'  => $data['Projectadd'],
            'Createman'   => $data['Createman'],
            'gcstatus'    => '表单处理完毕,等待开工',
            'gcbz'        => '开'
        ];

        $projectdata_result = Db::name('projectdata')->insert($projectdata);

        //将数据写入zuoyedata表
        $zuoyedata = [
            'Gcbh'        => $data['Gcbh'],
            'Sgdbh'       => $data['bh'],
            'Pdabh'       => $data['bh'].'^0001',
            'datatime'    => $data['datatime'],
            'MainPdaip'   => 'D200',
            'Senddate'    => $data['datatime'],
            'xianqu'      => $data['xianqu'],
            'xiangzhen'   => $data['xianqu'],
            'linename'    => $data['linename'],
            'Zyrenwu'     => $data['ProjectType'],
            'Jhstartdate'   => $data['startdate'],
            'Jhstarttime'   => $data['startdate'],
            'Jhenddate'   => $data['enddate'],
            'Jhendtime'   => $data['enddate'],
            'Inmans'     => 0,
            'Zypoints'   => 0,
            'status'    => '处理完毕'
        ];

        $zuoyedata_result = Db::name('zuoyedata')->insert($zuoyedata);

        //将数据写入zuoyesubdata表
        $zuoyesubdata_result = Db::name('zuoyesubdata')->insertall($data['subdata']);

        if (!$projectdata_result || !$zuoyedata_result || !$zuoyesubdata_result) {
            Db::rollback();
            $this->error('工程脱机数据写入失败');
        }

        Db::commit();
        $this->success('工程脱机数据写入成功');
    }


    public function test(){
         $data = [
        //projectdata表关键字段
        'bh'   =>'S20200309104437_X20200903165427_W20200903165954',//managerid,Sgdbh
        'Gcbh' =>'20200903188888',//工程编号
        'Projectname'=>'88千伏吴江', //工程名称
        'datatime' => '2020-11-13 ', //工程日期 
        'ProjectType'=>'新建',//工程类别
        'linename'=>'吴江线',//线路名称
        //'jhdate'=>'2020-09-03 8:0~2020-09-03 20:0',//计划时间 开工时间~竣工时间 2020-09-03 8:0~2020-09-03 20:0
        'startdate'=>'2020-09-03 8:0',
        'enddate'=>'2020-09-03 20:0',
        'Projectadd'=>'吴江',//工程地址
        'Createman'=>'吴江作业队',//创建人
        //zuoyedata表关键字段
        'xianqu'=>'吴江线', //xianqu,xiangzhen都是线路名称
        //zuoyesubdata 数据采集关键字段
        'subdata'=>[
            [
                'Gcbh'=>'20200903188888',//工程编号
                'ganhao'=>'吴江线_001',//杆塔号
                'Weidu'=>'39.8955321',//纬度
                'jingdu'=>'116.4077067',//经度
                'xuke'=>'Y',//许可点
                'caozuo'=>'Y',//操作点
                'jinzhi'=>'Y',//禁止点
                'duka'=>'Y',//危险点
                'kaiguan'=>'Y',//是否开关点
                'jiedi'=>'Y',//是否地线杆
                'zhixian'=>'N',//是否作业杆
                'jiaokua'=>'N',//是否交叉跨越点
                'bingxing'=>'N',//是否并行
                'lujing'=>'N',//是否班组地线杆
            ],
            [
                'Gcbh'=>'20200903188888',//工程编号
                'ganhao'=>'吴江线_002',//杆塔号
                'Weidu'=>'39.8951234',//纬度
                'jingdu'=>'116.4075678',//经度
                'xuke'=>'Y',//许可点
                'caozuo'=>'Y',//操作点
                'jinzhi'=>'Y',//禁止点
                'duka'=>'Y',//危险点
                'kaiguan'=>'N',//是否开关点
                'jiedi'=>'Y',//是否地线杆
                'zhixian'=>'N',//是否作业杆
                'jiaokua'=>'N',//是否交叉跨越点
                'bingxing'=>'N',//是否并行
                'lujing'=>'N',//是否班组地线杆
            ]
        ]
    ];

        $s = base64_encode(json_encode($data));
        echo $s;
    }

}